package love.yuanmengrobot.qianyugarden.controller;


import love.yuanmengrobot.qianyugarden.entity.User;
import love.yuanmengrobot.qianyugarden.entity.Yuanwen;
import love.yuanmengrobot.qianyugarden.service.BookService;
import love.yuanmengrobot.qianyugarden.service.UserEventsService;
import love.yuanmengrobot.qianyugarden.service.UserService;
import love.yuanmengrobot.qianyugarden.service.YuanwenService;
import love.yuanmengrobot.qianyugarden.utils.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.List;

import static love.yuanmengrobot.qianyugarden.utils.SystemConstant.grade;

@Controller
@RequestMapping("bus")
public class BusinessController {

    @Autowired
    UserEventsService userEventsService;
    @Autowired
    YuanwenService yuanwenService;
    @Autowired
    BookService bookService;

    /**
     * 跳转到首页
     *
     * @return
     */
    @GetMapping("/toindex")//转发的写法，即跳转到html
    public String toindex(HttpServletRequest request, ModelMap map) {
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
        map.put("css_nav", basePath + "css/nav.css");
//        map.put("css_nav","css/nav.css");//可用，备用
        map.put("css_index", basePath + "css/index.css");
        map.put("headportrait", basePath + "img/avatar.jpg");

        map.put("js_nav", basePath + "js/nav.js");
        map.put("index_html", basePath + "toMainContent");

        map.put("userId", null);
        map.put("userName", "888");
        map.put("userHistory", null);

//        用户id
        HttpSession session = request.getSession(false);
        if (session != null) {
            User user = (User) session.getAttribute("UserInfo");
            if (user != null) {
                map.put("userId", user.getObjectid());
                map.put("userName", user.getNickname());
                map.put("grade", null);
                map.put("headportrait", user.getHeadportrait());
                map.put("userHistory", userEventsService.queryAllYuanWenHistoryByLimit(user.getObjectid(), 0, 5));
                Integer userGrade = user.getGrade();
                if (userGrade != null) {
                    for (int ugrade = 0; ugrade < grade.length; ugrade++) {
                        if (userGrade < grade[ugrade]) {
                            map.put("grade", ugrade);
                            break;
                        }
                    }
                } else {
                    map.put("grade", 0);
                }
            }
        }

        return "index";
    }


    /**
     * 跳转到首页的主要内容区
     *
     * @return
     */
//    @GetMapping("/toMainContent")//转发的写法，即跳转到html
//    public String toMainContent(HttpServletRequest request, ModelMap map) {
//        String path = request.getContextPath();
//        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
//        map.put("css_mainContent", basePath + "css/mainContent/mainContent.css");
//        map.put("js_mainContent", basePath + "css/mainContent/carousel.css");
//        map.put("css_carousel", basePath + "js/mainContent/carousel.js");
//        map.put("js_carousel", basePath + "js/mainContent/mainContent.js");
//        System.out.println("首页");
//        return "/main/mainContent";
//    }

    /**
     * 跳转到登录界面
     *
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin(HttpServletRequest request, ModelMap map) {
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
        map.put("css_login", basePath + "css/login/login.css");
        map.put("css_button", basePath + "css/button.css");
        map.put("js_login", basePath + "js/login/login.js");
        map.put("js_button", basePath + "js/button.js");
        map.put("js_dialog", basePath + "js/Dialog.js");

        return "login/login";
    }

    /**
     * 跳转到注册界面
     *
     * @return
     */
    @RequestMapping("/toSignup")
    public String toSignup(HttpServletRequest request, ModelMap map) {
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
        map.put("css_signup", basePath + "css/login/signup.css");
        map.put("css_button", basePath + "css/button.css");
        map.put("js_signup", basePath + "js/login/signup.js");
        map.put("js_button", basePath + "js/button.js");
        map.put("js_dialog", basePath + "js/Dialog.js");
        return "/login/signup";
    }

    /**
     * 跳转到我的园子界面
     *
     * @return
     */
    @RequestMapping("/toMyGarden")
    public String toMyGarden() {
        return "redirect:/userEvents/loadMyGarden";
    }

    /**
     * 跳转到下载
     *
     * @return
     */
    @RequestMapping("/toBookList")
    public String toBookList() {
        return "redirect:/download/bookList";
    }

    /**
     * 跳转到检索结果
     *
     * @return
     */
    @GetMapping("/toFindResult")
    public String toFindResult(String temp ,ModelMap map ) {
        System.out.println("结果：" + temp);
        List<Yuanwen> yuanwens= yuanwenService.queryAllByString(temp);

        for(Yuanwen y:yuanwens){
            System.out.println("$:"+y);
        }
        map.put("yuanwenList",yuanwens);
        return "retrieval/retrieval";
    }


}
